// 在http.js中引入axios
import axios from 'axios' // 引入axios
import { toLoginPage } from '_projectFn/url'
import store from '_store/store'
const qs = require('qs')
// console.log('store', store)

const instance = new axios.create({
    baseURL: process.env.API,
    timeout: 10000,
    headers: {
        Token: store.state.user.ticket,
        Ticket: store.state.user.ticket
    }
})

// 请求响应拦截
instance.interceptors.response.use(
    response => {
        // console.log('response', response)
        if (response.status === 200) {
            if (response.data.errcode == 1100 || response.data.errcode == -2) { // 未登陆,跳转到登录页
                // console.log('登录信息失效, response:', response)
                toLoginPage({})
                return Promise.reject({
                    errcode: 2000
                })
            } else {
                return Promise.resolve(response.data)
            }
        }
    }
)

// Manages whether the request is repeated
class RequestManager {
    constructor() {
        this.getApiFlag = {}
    }

    // 根据生成的flag值是否为getApiFlag的属性来判断是否与正在请求的请求相同,否则返回flag值
    isRequesting({url, type, contentType = '', taskId}) {
        
        const flag = taskId || url + type + contentType // 请求的唯一标志
        // console.log('flag', flag)
        // console.log('taskId', taskId)
        // console.log('this.getApiFlag', this.getApiFlag)

        if (this.getApiFlag[flag]) return false // 属于重复执行，忽略此次请求

        this.addFlag(flag) // 添加标志

        return flag
    }

    // 添加标志,标志作为属性 值默认为1
    addFlag(flag, value = 1) {
        this.getApiFlag[flag] = value
    }

    // 删除属性
    deleteFlag(flag) {
        if (this.getApiFlag[flag]) delete this.getApiFlag[flag]
    }
}

const requestManagement = new RequestManager()

export function get({
    url, taskId, headers, baseURL, params, timeout, type = 'get'
}) {
    // console.log('get taskId:', taskId)
    const flag = requestManagement.isRequesting({url, type, taskId}) // 创建请求标志
    // console.log('flag', flag)
    // console.log('url', url)
    if (!flag || !url) {
        return Promise.reject({errcode: 2000})
    } else {
        return instance.get(url, {
            headers, baseURL, params, timeout
        })
        .then(res => {
            return Promise.resolve(res)
        }, res => {
            return Promise.reject(res)
        })
        .catch(res => {
            return Promise.reject(res)
        })
        .finally(() => {
            requestManagement.deleteFlag(flag)
        })
    }
}

export function post({
    url, taskId, params, methods = 'post', contentType = 'json', headers
}) {
    // console.log('post taskId:', taskId)
    const flag = requestManagement.isRequesting({url, type: methods, taskId}) // 创建请求标志
    // console.log('flag', flag)
    // console.log('url', url)
    // console.log('headers', headers)
    if (!flag || !url) {
        return Promise.reject({errcode: 2000})
    } else {
        // console.log('contentType', contentType)
        // console.log('params', params)
        params = contentType === 'json' ? params : qs.stringify(params)
        // console.log('params', params)
        return instance.post(url, params, {
            headers,
        })
        .then(res => {
            return res
        }, res => {
            return Promise.reject(res)
        })
        .catch(res => {
            return Promise.reject(res)
        })
        .finally(() => {
            requestManagement.deleteFlag(flag)
        })
    }
}